package com.threeti.mecool.core.domain.model.plan.pojo;

import com.threeti.mecool.core.infrastructure.persistence.plan.TaskActionDataTypeRepository;
import org.apache.commons.lang.Validate;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Configurable
@Entity
@Table(name = "DM_WF_TASK_ACTION_DATA_TYPE")
public class TaskActionDataType {
    public static final String CODE_INPUT_STORE_ADDRESS = "WF_TADT_ISA";
    public static final String CODE_INPUT_CONTACT_NAME = "WF_TADT_ICN";
    public static final String CODE_INPUT_CONTACT_POSITION = "WF_TADT_ICP";
    public static final String CODE_INPUT_CONTACT_WAY = "WF_TADT_ICW";

    private Long id;
    private Integer version;
    private String name;
    private String code;
    private int actionId;
    private Date updatedAt;
    private Integer removed;

    private TaskActionDataTypeRepository taskActionDataTypeRepository;

    private static TaskActionDataTypeRepository taskActionDataTypeRepository() {
        return new TaskActionDataType().taskActionDataTypeRepository;
    }

    @Transactional
    public void save() {
        updatedAt = new Date();
        taskActionDataTypeRepository.saveAndFlush(this);
    }

    @Transactional
    public void remove() {
        removed = 1;
        save();
    }

    public static TaskActionDataType findById(Long id) {
        return taskActionDataTypeRepository().findOne(id);
    }

    public static List<TaskActionDataType> findByActionId(int actionId) {
        return taskActionDataTypeRepository().findByActionId(actionId);
    }

    public static TaskActionDataType findByCode(String code) {
      Validate.notNull(code, "TaskActionDataType Code不能为null");
      return taskActionDataTypeRepository().findByCode(code);
    }

    public static List<TaskActionDataType> findByCodes(String... codes) {
        return taskActionDataTypeRepository().findByCodeIn(codes);
    }

    @Deprecated
    public static List<TaskActionDataType> findByActionIdAndName(int actionId, String name) {
        return taskActionDataTypeRepository().findByActionIdAndName(actionId, name);
    }

//    @Deprecated
//    public static List<TaskActionDataType> findByActionIdAndNames(int actionId, String... names) {
//        return taskActionDataTypeRepository().findByActionIdAndNameIn(actionId, names);
//    }

    public static TaskActionDataType findFirstByActionIdAndName(int actionId, String name) {
        List<TaskActionDataType> list = findByActionIdAndName(actionId, name);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public TaskActionDataType() {
    }

    public TaskActionDataType(int actionId, String name) {
        this();
        this.actionId = actionId;
        this.name = name;
    }

    public TaskActionDataType(int actionId, String name, String code) {
        this();
        this.actionId = actionId;
        this.name = name;
        this.code = code;
    }

    //<editor-fold desc="getters/setters">

    @Transient
    @JsonIgnore
    public TaskActionDataTypeRepository getTaskActionDataTypeRepository() {
        return taskActionDataTypeRepository;
    }

    @Resource
    public void setTaskActionDataTypeRepository(TaskActionDataTypeRepository taskActionDataTypeRepository) {
        this.taskActionDataTypeRepository = taskActionDataTypeRepository;
    }

    public int getActionId() {
        return actionId;
    }

    public void setActionId(int actionId) {
        this.actionId = actionId;
    }

    @NotNull
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(unique=true)
    public String getCode() {
      return code;
    }

    public void setCode(String code) {
      this.code = code;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @JsonIgnore
    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    @JsonIgnore
    public Integer getRemoved() {
        return removed;
    }

    public void setRemoved(Integer removed) {
        this.removed = removed;
    }
//</editor-fold>
}
